package com.irun.map;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

public class MainActivity extends MapActivity {

	private MapView mMapView;
	private MapController mMapController;
	private MyOverLay mOverLay;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map_layout);
        
        mMapView = (MapView) findViewById(R.id.mapview);
        //交通模式
        mMapView.setTraffic(true);
        //卫星
        mMapView.setSatellite(false);
        //街景
        mMapView.setStreetView(false);
        mMapView.setEnabled(true);
        
        mMapController = mMapView.getController();
        mMapController.setZoom(12);
        
        mOverLay = new MyOverLay();
        List<Overlay> list = mMapView.getOverlays();
        list.add(mOverLay);
        
        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        
        Criteria criteria=new Criteria();  
        criteria.setAccuracy(Criteria.ACCURACY_FINE);  
        criteria.setAltitudeRequired(false);  
        criteria.setBearingRequired(false);  
        criteria.setCostAllowed(false);  
        criteria.setPowerRequirement(Criteria.POWER_LOW);  
        //取得效果最好的Criteria  
        String provider=locationManager.getBestProvider(criteria, true);  
        //得到Location  
        Location location=locationManager.getLastKnownLocation(provider);  
        updateWithLocation(location);  
        //注册一个周期性的更新，3秒一次  
        locationManager.requestLocationUpdates(provider, 3000, 0, mLocationListener);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    private void updateWithLocation(Location location){  
        if(location!=null){  
            //为绘制类设置坐标  
        	mOverLay.setLocation(location);  
            GeoPoint geoPoint=new GeoPoint((int)(location.getLatitude()*1E6), (int)(location.getLongitude()*1E6));  
            //定位到指定的坐标  
            mMapController.animateTo(geoPoint);  
        }  
    }  
    
    private LocationListener mLocationListener = new LocationListener() {
		
		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
			
		}
		
		@Override
		public void onProviderEnabled(String provider) {
			
		}
		
		@Override
		public void onProviderDisabled(String provider) {
			
		}
		
		@Override
		public void onLocationChanged(Location location) {
			updateWithLocation(location);
		}
	};
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
       
        return super.onOptionsItemSelected(item);
    }

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	class MyOverLay extends Overlay{  
        
        private Location location;  
        public void setLocation(Location location){  
            this.location=location;  
        }  
          
        @Override  
        public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {  
            super.draw(canvas, mapView, shadow);  
            Paint paint=new Paint();  
            Point myScreen=new Point();  
            //将经纬度换成实际屏幕的坐标。  
            GeoPoint geoPoint=new GeoPoint((int)(location.getLatitude()*1E6), (int)(location.getLongitude()*1E6));  
            mapView.getProjection().toPixels(geoPoint, myScreen);  
            paint.setStrokeWidth(1);  
            paint.setARGB(255, 255, 0, 0);  
            paint.setStyle(Paint.Style.STROKE);  
            Bitmap bmp=BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);  
            //把这张图片画到相应的位置。  
            canvas.drawBitmap(bmp, myScreen.x, myScreen.y,paint);  
            canvas.drawText("here", myScreen.x, myScreen.y, paint);  
            return true;  
        }  
    }
}
